package com.techysky.repository.impl;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import com.techysky.repository.CoreRepository;

/**
 * * * @author zhangxu * @date 2016年11月11日 上午9:36:28 * @remark 公用持久层实现 * @param
 * <T> * @param <ID>
 */
public class CoreRepositoryImpl<T, I extends Serializable> extends SimpleJpaRepository<T, I>
		implements CoreRepository<T, I> {
	private final EntityManager em;
	private final String domainClassName;

	public CoreRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager em) {
		super(entityInformation, em);
		this.em = em;
		this.domainClassName = entityInformation.getJavaType().getName();
	}

	public CoreRepositoryImpl(Class<T> domainClass, EntityManager em) {
		super(domainClass, em);
		this.em = em;
		this.domainClassName = domainClass.getName();
	}

	@Override
	public boolean support(String modelType) {
		return this.domainClassName.equals(modelType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findByCode(String code) {
		Query query = em.createQuery(" from " + this.domainClassName + " t where t.code=:code order by t.orderCode ");
		query.setParameter("code", code);
		return query.getResultList();
	}
}